package com.xy.lang.domain;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 *
 */
public class PageRequest implements Serializable {

    private static final long serialVersionUID = -1L;

    public final static int DEFAULT_PAGE_SIZE = 20;

    /**
     * page index
     */
    private int pageIndex;

    /**
     * page size
     */
    private int pageSize = DEFAULT_PAGE_SIZE;

    /**
     *
     */
    public PageRequest() {
        this(0, DEFAULT_PAGE_SIZE);
    }

    /**
     *
     * @param pageSize
     */
    public PageRequest(int pageSize) {
        this(0, pageSize);
    }

    /**
     *
     * @param pageIndex
     * @param pageSize
     */
    public PageRequest(int pageIndex, int pageSize) {
        checkIndex(pageIndex);
        checkSize(pageSize);
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }

    /**
     *
     * @param pageIndex
     * @return
     */
    public PageRequest index(int pageIndex) {
        checkIndex(pageIndex);
        setPageIndex(pageIndex);
        return this;
    }

    /**
     *
     * @param pageSize
     * @return
     */
    public PageRequest size(int pageSize) {
        checkSize(pageSize);
        setPageSize(pageSize);
        return this;
    }

    @Override
    public String toString() {
        return "PageRequest{" +
                "pageIndex=" + pageIndex +
                ", pageSize=" + pageSize +
                '}';
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        checkIndex(pageIndex);
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        checkSize(pageSize);
        this.pageSize = pageSize;
    }


    private void checkIndex(int pageIndex) {
        if (pageIndex < 0) {
            throw new IndexOutOfBoundsException("The pageIndex should be than -1.");
        }
    }

    private void checkSize(int pageSize) {
        if (pageSize < 1) {
            throw new IndexOutOfBoundsException("The pageSize should be than 0.");
        }
    }

}
